package cn.tm.ms.memory.db;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import cn.tm.ms.memory.Memory;

public class MemoryFactory {

	private MemoryFactory() {
	}

	private static class SingletonHolder {
		public static final Memory MEMORY = new Memory(new SimpleDataSource());		
	}

	public static Memory getInstance() {
		return SingletonHolder.MEMORY;
	}

	/**
	 * 在容器(Tomcat)运行状态下，可使用getDataSource()
	 */
	public static final DataSource getDataSource() {
		try {
			Context context = new InitialContext();
			return (DataSource) context.lookup("java:comp/env/jdbc/test");
		} catch (NamingException e) {
			throw new RuntimeException(e);
		}
	}
}
